package com.quxue.im.controller;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import com.quxue.im.exception.LogicException;
import com.quxue.im.util.UploadUtils;
import com.quxue.im.vo.AjaxResult;
import com.quxue.im.vo.upload.UploadFileVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 文件处理类
 */
@RestController
@RequestMapping("/file")
public class FileController {


    @Resource
    private UploadUtils uploadUtils;

    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        if (file == null) {
            throw new LogicException("上传文件不能为空");
        }

        String fileType = FileNameUtil.extName(file.getOriginalFilename());
        if ("webp".equalsIgnoreCase(fileType)) {
            throw new LogicException(StrUtil.format("暂不支持{}格式上传", fileType));
        }
        // 上传
        UploadFileVo uploadFileVo = uploadUtils.uploadFile(file);

        return AjaxResult.success(uploadFileVo);
    }
}
